package com.swan.controllers;

import java.io.IOException;

import javafx.event.ActionEvent;
import javafx.stage.Stage;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.PasswordField;
import javafx.scene.control.TextField;

import com.swan.beans.UserBean;
import com.swan.beans.UserInfo;
import com.swan.exceptions.CustomerException;
import com.swan.models.LoginDao;
import com.swan.models.UserDao;
import com.swan.views.MainApplication;

/**
 * LoginController
 * 
 * @author xyh
 * @param <T>
 * @date 20230426
 *
 */

public class LoginController {
	@FXML
	private TextField AccountTextField;
	@FXML
	private PasswordField PasswordPasswordField;
	@FXML
	private Label MessageLabel;
	@FXML
	private Button LoginButton;

	@FXML
	private PasswordField oldPwdField;

	@FXML
	private PasswordField newPwdField;

	@FXML
	private PasswordField newPwdAgainField;

	private LoginDao dao = new LoginDao();

	private UserDao daoUser = new UserDao();

	public void loginButtonClick(ActionEvent event) throws IOException {
		String account = AccountTextField.getText();
		String password = PasswordPasswordField.getText();
		System.out.println("account:" + account + ",password:" + password);

		if (account != null && !account.equals("") && password != null && !password.equals("")) {
			try {
				int login = dao.login(account, password);
				if (login > 0) {

					MessageLabel.setText("login success!");

					// save login information
					UserInfo.getInstace(account, login);

					UserBean userBean = daoUser.getUserById(login);
					String role = userBean.getRole();

					Stage primaryStage = (Stage) LoginButton.getScene().getWindow();
					primaryStage.close();
					MainApplication mainApplication = new MainApplication();

					switch (role) {
					case "ADMIN":
						mainApplication.mainStage("Admin");
						break;

					case "TEACHER":
						mainApplication.mainStage("Teacher");
						break;

					case "STUDENT":
						mainApplication.mainStage("Student");
						break;

					}
				}
			} catch (CustomerException e) {
				MessageLabel.setText("error account or password!");
			}
		} else {
			MessageLabel.setText("type your account or password!");
		}
	}

	public void changePwd(ActionEvent event) throws IOException {
		String oldPwd = oldPwdField.getText();
		String newPwd = newPwdField.getText();
		String newPwdAgain = newPwdAgainField.getText();
		if (oldPwd != null && !oldPwd.equals("") && newPwd != null && !newPwd.equals("") && newPwdAgain != null
				&& !newPwdAgain.equals("")) {
			if (newPwd.equals(newPwdAgain)) {
				UserInfo loginUser = UserInfo.getLoginUser();
				if (loginUser != null) {
					String userName = loginUser.getUserName();
					int login = dao.login(userName, oldPwd);
					if (login > 0) {
						dao.changePassword(userName, oldPwd, newPwd);
						MessageLabel.setText("new password has been set !");
					} else {
						MessageLabel.setText("old password is wrong!");
					}
				}
			} else {
				MessageLabel.setText("new password not equals new password again!");
			}
		} else {
			MessageLabel.setText("can not null!");
		}

	}

}
